package com.efast.dmom.mgmt.service.mes.report.impl;

import java.text.NumberFormat;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.efast.cafe.commponent.query.service.impl.QueryServiceImpl;
import com.efast.cafe.framework.bean.QueryPageBase;
import com.efast.cafe.portal.bean.query.QueryBean;
import com.efast.cafe.portal.bean.query.QueryConditionBean;
import com.efast.cafe.portal.bean.query.QueryOpBean;
import com.efast.dmom.mgmt.bean.mes.report.EachQualityGateDoorIndexBean;
import com.efast.dmom.mgmt.dao.mes.report.ReportVehicleRetrospectDao;
import com.efast.dmom.mgmt.utils.Assist;

import jodd.util.StringUtil;

/**
 * 重写各质量门一次交检合格率通用查询方法
 * 
 * @author Administrator
 *
 */
@Service("apiindex533-1097")
public class EachQualityGateDoorFTQServiceImpl extends QueryServiceImpl {

	@Autowired
	private ReportVehicleRetrospectDao reportVehicleRetrospectDao;

	@Override
	public Object queryList(QueryBean querys) {
		QueryPageBase result = new QueryPageBase();
		QueryConditionBean queryConditionBean = querys.getQueryConditionBean();
		Assist assist = new Assist();
		if (queryConditionBean != null) {
			if (queryConditionBean.getStart() == null || queryConditionBean.getLength() == null) {
				queryConditionBean.setStart(0);
				queryConditionBean.setLength(querys.getTemplate().getPagenation());
			}
			assist.setStartRow(queryConditionBean.getStart());
			assist.setRowSize(queryConditionBean.getLength());
			List<QueryOpBean> queryOpBeans = queryConditionBean.getQueryOpBeans();
			if (queryOpBeans != null && queryOpBeans.size() > 0) {
				for (QueryOpBean data : queryOpBeans) {
					if (StringUtil.isNotBlank(data.getKey()) && StringUtil.isNotBlank(data.getValue())) {
						if (data.getOpName().equals("包含")) {
							assist.setRequires(Assist.andLike(data.getKey(), "%" + data.getValue() + "%"));
						}
						if (data.getOpName().equals("等于")) {
							assist.setRequires(Assist.andEq(data.getKey(), data.getValue()));
						}
						if (data.getOpName().equals("介于(包含)")) {
							String[] split = data.getValue().split("~");
							String beginTime = "";
							String endTime = "";
							if (split != null && split.length > 1) {
								beginTime = split[0];
								endTime = split[1];
							}
							assist.setRequires(Assist.andGte(data.getKey(), beginTime));
							assist.setRequires(Assist.andLte(data.getKey(), endTime));
						}
					}
				}
			}
			// 获取每天、每周、每月通过各质量门检验的质量缺陷总项次、以及检验的总台数
			NumberFormat numberFormat = NumberFormat.getInstance();
			numberFormat.setMaximumFractionDigits(2);
			List<EachQualityGateDoorIndexBean> eachQualityGateDoorIndexBean = reportVehicleRetrospectDao
					.selectQtyAndDefectqtyByFTQ(assist);

			int countSize = reportVehicleRetrospectDao
					.selectQtyAndDefectqtyByFTQCount(assist);
            if (eachQualityGateDoorIndexBean != null && eachQualityGateDoorIndexBean.size() > 0) {
            	for (EachQualityGateDoorIndexBean data:eachQualityGateDoorIndexBean) {
            		data.setDayFTQ(numberFormat.format(data.getDayFTQData() * 100) + "%");
            		data.setWeekFTQ(numberFormat.format(data.getWeekFTQData() * 100) + "%");
					data.setMonthFTQ(numberFormat.format(data.getMonthFTQData() * 100) + "%");
            	}
            }
			result.setResultList(eachQualityGateDoorIndexBean);
			result.setTotalRows(countSize);
		}
		return result;
	}
}
